package com.tangula.voicenote.view

import android.content.Context
import android.media.MediaPlayer
import android.media.MediaRecorder
import android.os.Environment
import android.support.constraint.ConstraintLayout
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.*
import com.tangula.voicenote.R
import java.io.IOException
import com.tangula.android.utils.ToastUtils
import com.tangula.voicenote.adapter.ListViewAdapter


class NoteShowView(internal var c: Context) : View(c) {
    internal  var view: View = LayoutInflater.from(c).inflate(R.layout.layout_note, null)
    var mRecoder: MediaRecorder? = null
    var mMediaPlayer: MediaPlayer? = null
    var fileName: String? = null
    var filePath: String? = null
    var root:ConstraintLayout? = null
    var add: ImageView? = null
    var down: ImageView? = null
    var sound: ImageView? = null
    var nosound: ImageView? = null
    var close: ImageView? = null
    var duration: Button? = null
    var text: EditText? = null
    var hold: Button? = null
    var list:ListView? = null



    init {
        root = view.findViewById<ConstraintLayout>(R.id.vw_root)
        root!!.background = resources.getDrawable(R.drawable.ic_launcher_background_pink)

        fileName = Environment.getExternalStorageDirectory().absolutePath
        filePath = "$fileName/audiorecordtest.3gp"

        mRecoder = MediaRecorder()
        mRecoder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
        mRecoder!!.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB)
        mRecoder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
        mRecoder!!.setOutputFile(filePath)
        mMediaPlayer = MediaPlayer()


        add = view.findViewById(R.id.vw_img_add)
        this.add!!.setImageResource(R.mipmap.add)

        down = view.findViewById(R.id.vw_img_down)
        down!!.setImageResource(R.mipmap.down)

        sound = view.findViewById(R.id.vw_img_sound)
        sound!!.setImageResource(R.mipmap.sound)

        nosound = view.findViewById(R.id.vw_img_nosound)
        nosound!!.setImageResource(R.mipmap.nosound)
        nosound!!.visibility = View.GONE

        close = view.findViewById(R.id.vw_img_close)
        close!!.setImageResource(R.mipmap.close)

        duration = view.findViewById(R.id.vw_duration_button)
        duration!!.visibility = View.GONE

        text = view.findViewById(R.id.editText)

        hold = view.findViewById(R.id.vw_hold_to_talk_button)
        hold!!.visibility = View.GONE

        list = view.findViewById(R.id.vw_list)
//        val data = arrayOf(
//            "Copy",
//            "Paste",
//            "Share",
//            "Cancle",
//            "Go Ad Free"
//        ).toMutableList()
//        val toolsAdapter = ListViewAdapter(c,R.layout.item_list,data)
        list = view.findViewById(R.id.vw_list)
//        list!!.adapter = toolsAdapter

        list!!.visibility = View.GONE

        down!!.setOnClickListener {
            if(list!!.visibility == View.GONE){
                list!!.visibility = View.VISIBLE
            }
            else{
                list!!.visibility = View.GONE
            }
        }
        sound!!.setOnClickListener {
            sound!!.visibility = View.GONE
            nosound!!.visibility = View.VISIBLE
            hold!!.visibility = View.VISIBLE
        }
        nosound!!.setOnClickListener {
            sound!!.visibility = View.VISIBLE
            nosound!!.visibility = View.GONE
            hold!!.visibility = View.GONE
        }
        var startTime:Long = 0
        var stopTime:Long = 0
        hold!!.setOnTouchListener { v, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    startTime = event.downTime
                    if (mRecoder == null) {
                        mRecoder = MediaRecorder()
                        mRecoder!!.setAudioSource(MediaRecorder.AudioSource.MIC)
                        mRecoder!!.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
                        mRecoder!!.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
                        mRecoder!!.setOutputFile(filePath)
                        try {
                            mRecoder!!.prepare()
                            mRecoder!!.start()
                        } catch (e: IllegalStateException) {
                            print(e.message)
                        } catch (e: IOException) {
                            print(e.message)
                        }
                    }
                }
                MotionEvent.ACTION_MOVE ->{
                }
                MotionEvent.ACTION_UP -> {
                    stopTime = event.eventTime
                    if(stopTime-startTime < 300){
                        ToastUtils.showToastShort("The recording time is too short.")
                    }else{
                        if (mRecoder != null) {
                            try {
                                mRecoder!!.stop()
                            } catch (e: IllegalStateException) {
                                mRecoder = null
                                mRecoder = MediaRecorder()
                            }

                            mRecoder!!.release()
                            mRecoder = null
                        }
                        duration!!.visibility = View.VISIBLE
                    }
                }
            }
            return@setOnTouchListener true
        }
        duration!!.setOnClickListener {
            if (mMediaPlayer!!.isPlaying()) {
                mMediaPlayer!!.stop()
            }
            mMediaPlayer!!.reset()
            try {
                mMediaPlayer!!.setDataSource(filePath)
                mMediaPlayer!!.prepare()
                mMediaPlayer!!.start()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }
    }
}
